#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void take(void)
{
	
	char path[PATH_MAX];
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("取款：acc path:%s\n",path);
	int fd = open(path,O_RDWR);
	Ctos take;
	read(fd,&take.acc,sizeof(take.acc));	
	
	if(take.acc.money>=ctos.acc.money)	
	{
		take.acc.money = take.acc.money - ctos.acc.money;	
		sprintf(stoc.str,"恭喜您取款成功，取款金额为:%f,帐号余额为：%f ",ctos.acc.money,take.acc.money);
				
	}
	else sprintf(stoc.str,"余额不足");
	// 返回取款成功信息
	lseek(fd,0,SEEK_SET);
	write(fd,&take.acc,sizeof(take.acc));
	close(fd);
	stoc_send(&stoc);
}


int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_TAKE);
		stoc.type = ctos.pid;
		take();
	}
}
